/* main.c */

#include <avr/io.h>
#include "delay.h"
#include "serial.h"
#include "usart.h"

#define SERIAL_DDR DDRB
#define SERIAL_PORT PORTB
#define SERIAL_PIN PB3

#define UBRR (16000000 / 16 / 9600 - 1)

int main(void)
{
   usart_init(UBRR);
   
   usart_tx_string("\r\n");
   usart_tx_string("    _            _       _\r\n");
   usart_tx_string("   / \\   _ __ __| |_   _(_)_ __   ___\r\n");
   usart_tx_string("  / _ \\ | '__/ _` | | | | | '_ \\ / _ \\\r\n");
   usart_tx_string(" / ___ \\| | | (_| | |_| | | | | | (_) |\r\n");
   usart_tx_string("/_/   \\_\\_|  \\__,_|\\__,_|_|_| |_|\\___/\r\n");
   usart_tx_string("\r\n");

   serial_init(&SERIAL_DDR, SERIAL_PIN);

   serial_tx(&SERIAL_PORT, SERIAL_PIN, 22);
   delay_ms(5);
   
   while (1) {
      char c = usart_rx();
      if (c == '\r') {
         serial_tx(&SERIAL_PORT, SERIAL_PIN, 12);
         delay_ms(5);
      } else if (c == '!') {
         serial_tx_string(&SERIAL_PORT, SERIAL_PIN, "Mooo!");
      } else {
         serial_tx(&SERIAL_PORT, SERIAL_PIN, c);
      }
   }
   
   return 0;
}
